

import 'package:syria_sports/core/app_export.dart';
import 'package:syria_sports/data/apiClient/apis_data.dart';
import 'package:syria_sports/data/models/error_model.dart';
import '../../core/utils/show_toast.dart';
import '../../presentation/attendances_list_page/models/attendance.dart';
import '../apiClient/api_client.dart';
import '../apiClient/api_keys.dart';
import '../models/auth/user.dart';
import '../models/data.dart';
import '../remote/dio_helper.dart';

class AttendancesController{

  static Future<Data?> getAllAttendances()async{
    var res = await DioHelper.getData( url: ApiClient.getAttendances, bearerToken: PrefUtils.getToken());

    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
      return null;
    }else{
      // showMessage(res.data[ApiKeys.message]);
    }
    Data response = Data.fromJson(res.data);
    return response;
  }

  static Future<Data?> getAttendancesByDate({
    required DateTime startDate,
    required  DateTime endDate,
    String? exerciseId,
  })async{
    var res = await DioHelper.getData(
        url: ApiClient.getAttendancesByDate,
        bearerToken: PrefUtils.getToken(),
      query: {
          'start_date': startDate.format(),
          'end_date': endDate.format(),
        if(exerciseId!= null)
          'exercise_id':exerciseId,
      }
    );

    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
      return null;
    }else{
      //showMessage(res.data.toString());
    }
    Data response = Data.fromJson(res.data);
    return response;
  }

  static Future<Data?> getUserAttendances({
    required User user,
  })async{
    var res = await DioHelper.getData(
        url: ApiClient.getUserAttendances,
        bearerToken: PrefUtils.getToken(),
        query: {
          'id': user.userId,
        }
    );

    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
      return null;
    }else{
      //showMessage(res.data.toString());
    }
    Data response = Data.fromJson(res.data);
    return response;
  }

  static Future<Data?> addAttendance(Attendance attendance)async{
    var res = await DioHelper.postData(
      url: ApiClient.attendance,
      data: attendance.toJson(),
      bearerToken: PrefUtils.getToken(),
    );
    if(res == null)return null;
    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
    }else{
      showMessage(res.data[ApiKeys.message]);
    }
    return null;
  }

  static Future<Data?> getAttendanceById(int id)async{
    var res = await DioHelper.getData(
      url: ApiClient.subscription,
      query: {
        'id':id
      },
      bearerToken: PrefUtils.getToken(),
    );
    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
    }else if(!res.data[ApiKeys.state]){
      showMessage(res.data[ApiKeys.message]);
    }else{
      Data response = Data.fromJson(res.data);
      return response;
    }
    return null;
  }

  static Future<Data?> deleteById(String id)async{
    var res = await DioHelper.postData(
      url: ApiClient.attendanceDelete,
      data: {
        'id':id
      },
      query: {
        'id':id,
      },
      bearerToken: PrefUtils.getToken(),
    );
    if(res == null)return null;
    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
    }else{
      showMessage(res.data[ApiKeys.message]);
    }

    return null;
  }
}